//
//  RANetWorkServiceController.m
//  RayoSmartBleKey
//
//  Created by Piccolo on 2017/7/19.
//  Copyright © 2017年 Piccolo. All rights reserved.
//

#import "RANetWorkServiceController.h"

@implementation RANetWorkServiceController

//实现单例方法
+ (instancetype)sharedManager{
    
    static RANetWorkServiceController *tool = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        tool = [[self alloc] init];
    });
    return tool;
}

- (void)startNetWorkDetectionService{
    
    if (!_netWorkModel) {
        _netWorkModel = [RANetWorkModel new];
        [self startDetectNetworkStatus];
    }
}

- (void)startDetectNetworkStatus {
    
    //1.创建网络监测者
    AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
    
    /*枚举里面四个状态  分别对应 未知 无网络 数据 WiFi
     typedef NS_ENUM(NSInteger, AFNetworkReachabilityStatus) {
     AFNetworkReachabilityStatusUnknown          = -1,      未知
     AFNetworkReachabilityStatusNotReachable     = 0,       无网络
     AFNetworkReachabilityStatusReachableViaWWAN = 1,       蜂窝数据网络
     AFNetworkReachabilityStatusReachableViaWiFi = 2,       WiFi
     };
     */
    
    [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        
        BOOL netWorkOn = NO;
        switch (status) {
            case AFNetworkReachabilityStatusUnknown:
                netWorkOn = NO;
                _netWorkModel.netWorkType = NSRANetWorkUnknowType;
                NSLog(@"未知网络状态");
                break;
            case AFNetworkReachabilityStatusNotReachable:
                netWorkOn = NO;
                _netWorkModel.netWorkType = NSRANetWorkOfflineType;
                NSLog(@"无网络");
                break;
                
            case AFNetworkReachabilityStatusReachableViaWWAN:
                netWorkOn = YES;
                _netWorkModel.netWorkType = NSRANetWorkWWANType;
                NSLog(@"蜂窝数据网");
                break;
                
            case AFNetworkReachabilityStatusReachableViaWiFi:
                netWorkOn = YES;
                _netWorkModel.netWorkType = NSRANetWorkWiFiType;
                NSLog(@"WiFi网络");
                break;
                
            default:
                netWorkOn = NO;
                break;
        }
        
        _netWorkModel.netWorkOn = netWorkOn;
    }] ;
    [manager startMonitoring];
}






@end
